<template>
  <div class="home">
    <!-- 头部导航栏 -->
    <div class="header">
      <div class="headerContent">
        <div class="headerLeft">
          <img src="@/assets/logos.png" alt="logos" class="logos">
          <p class="appName">多功能物联网平台</p>
        </div>
        <div class="headerRight">
          <el-dropdown @command="handleCommand">
            <span>
              {{ userName }}
              <el-icon>
                <arrow-down />
              </el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="manage">管理设置</el-dropdown-item>
                <el-dropdown-item command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
    </div>
    <!-- 内容 -->
    <div class="body">
      <div class="content">
        <!-- 输入框 -->
        <div class="searchInput">
          <div class="searchContent">
            <el-input v-model="searchInput" placeholder="Please input" clearable @focus="inputFocus" @blur="inputBlur">
              <template #append>
                <el-button :icon="Search" @click="submitSearch" />
              </template>
            </el-input>
            <div :class="{ tips: true, hidden: focus == false }">
              <p class="tip" v-for="item in historySearch" @click="setInput(item)">{{ item }}</p>
              <p class="tip" v-if="historySearch.length == 0">暂无历史记录</p>
              <p class="clearHistorySearch" @click="clearHistorySearch">清空历史记录</p>
            </div>
          </div>
        </div>
        <!-- 标签页 -->
        <div class="tag">
          <div class="selectCompany">
            <div class="selectCompanyLeft">
              <div class="selectCompanyLeftTxt">
                所属地区
              </div>
            </div>
            <div class="selectCompanyRight">
              <div :class="{ selectTag: true, selected: index == selectedAreaIdx }" v-for="(item, index) in areaType"
                @click="selectAreaType(index)">
                {{ item }}
              </div>
            </div>
          </div>
          <div class="selectCompany">
            <div class="selectCompanyLeft">
              <div class="selectCompanyLeftTxt">
                所属公司
              </div>
            </div>
            <div class="selectCompanyRight">
              <div :class="{ selectTag: true, selected: index == selectedCompanyIdx }"
                v-for="(item, index) in companyType" @click="selectCompanyType(index)">
                {{ item.companyName }}
              </div>
            </div>
          </div>
        </div>
        <!-- 权限公司入口标签 -->
        <div class="companyList">
          <div :class="{ company: true, company_left: index % 2 == 0, company_right: index % 2 == 1 }"
            v-for="(item, index) in companyList.slice(pageSize * (pageNo - 1), pageSize * pageNo)" @click="toBusiness(item.id)">
            <el-icon size="50" v-if="item.imageUrl == ''">
              <HomeFilled />
            </el-icon>
            <img :src="item.imageUrl" alt="公司logos" v-else style="width: 50px;height: 50px;">
            <div class="companyInfo">
              <p class="infoName">{{ item.companyName }}</p>
              <p class="address">{{ item.companyAddress }}</p>
            </div>
          </div>
        </div>
        <div class="pagination">
          <el-pagination background layout="prev, pager, next" :total="companyList.length" :page-size="pageSize"
            @current-change="changePage" />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref, nextTick, onMounted, toRaw, computed, onUpdated } from 'vue'
import useUserStore from '@/store/user.js'
import { reqHomeData } from '../../api/user';
import { useRoute, useRouter } from 'vue-router';
import useSearchHistoryStore from '../../store/searchHistory';
let userName = useUserStore().userInfo?.userName
let searchInput = ref("")
let historySearch = ref([])
let pageNo = ref(1)
let pageSize = ref(10)
let historyStore=useSearchHistoryStore()
// 地区tag
let areaType = reactive([
  "全部", "北京", "天津", "上海", "重庆",
  "河北", "山西", "辽宁", "吉林", "黑龙江",
  "江苏", "浙江", "安徽", "福建", "江西", "山东",
  "河南", "湖北", "湖南", "广东", "海南",
  "四川", "贵州", "云南", "陕西", "甘肃",
  "青海", "台湾",
  "内蒙古", "广西", "西藏", "宁夏", "新疆",
  "香港", "澳门"
])
//总公司tag名
let companyType = reactive([{ companyName: "全部", id: 0 }])
// 条件筛选下公司
let companyList = computed(() => {
  let all = toRaw(companies)
  if (companyType[selectedCompanyIdx.value].companyName != "全部") {
    all = all.filter((item) => item.companyParentId == companyType[selectedCompanyIdx.value].id)
  }
  if (areaType[selectedAreaIdx.value] != "全部") {
    all = all.filter((item) => item.companyAddress.includes(areaType[selectedAreaIdx.value]))
  }
  if (searchWord.value != "") {
    all = all.filter((item) => item.companyName.includes(searchWord.value))
  }
  return all
})

// 权限下所有公司
let companies = reactive([])
// 输入框聚焦
let focus = ref(false)
//总公司tag位置
let selectedCompanyIdx = ref(0)
// 地区tag位置
let selectedAreaIdx = ref(0)
//搜索关键字
let searchWord = ref("")
let $route = useRoute()
let $router = useRouter()

// 下拉框处理
const handleCommand = (command) => {
  switch (command) {
    case 'logout':
      useUserStore().logout()
      $router.push("/login")
      break;
    case 'manage':
      $router.push("/manage/device")
      break
    default:
      
  }
};

const changePage = (newPage) => {
  pageNo.value = newPage
}
// 输入焦点
const inputFocus = () => {
  focus.value = true
}
// 输入失焦
const inputBlur = () => {
  setTimeout(() => {
    focus.value = false
  }, 100)
}

const setInput = (item) => {
  searchInput.value = item
}
// 清空历史记录
const clearHistorySearch = () => {
  historyStore.clear()
}
// 公司tag选择
const selectCompanyType = (index) => {
  selectedCompanyIdx.value = index
}
// 地区tag选择
const selectAreaType = (index) => {
  selectedAreaIdx.value = index
}
// 查询提交
const submitSearch = () => {
  searchWord.value = searchInput.value
  historyStore.append(searchInput.value)
}

const toBusiness=(companyId)=>{
  $router.push({
    path:'/business',
    query:{
      companyId:companyId
    }
  })
}

onMounted(async () => {
  historySearch.value=historyStore.history
  // 数据请求
  let res = await reqHomeData()
  // code 0:成功引入，1：token过期，2：其它错误
  switch (res.code) {
    case 0:
      companyType.push(...(res.data.companyParent.map(item => ({ companyName: item.companyName, id: item.id }))))
      companies.push(...res.data.company)
      break;
    case 1:
      setTimeout(() => { $router.push('/login') }, 1000)
      break;
  }
})

onUpdated(()=>{
  historySearch.value=historyStore.history
})
</script>

<style scoped lang="scss">
.hidden {
  display: none;
}

.home {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  // justify-content: center;

  .header {
    width: 100%;
    height: 50px;
    border-bottom: 0.5px solid #eacbbe;
    box-sizing: border-box;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #f3ddd4;

    .headerContent {
      display: flex;
      width: 60%;
      height: 100%;
      justify-content: space-between;
      align-items: center;

      .headerLeft {
        display: flex;
        box-sizing: border-box;
        width: 250px;
        align-items: center;

        @media (max-width: 500px) {
          p {
            display: none;
            /* 隐藏段落 */
          }
        }

        .logos {
          height: 50px;
          width: 50px;
        }

        .appName {
          margin-left: 10px;
          font-size: 20px;
          font-weight: 700;
          overflow: hidden;
          white-space: nowrap;
        }

      }

      .headerRight {
        display: flex;

        :deep(.el-tooltip__trigger:focus-visible) {
          outline: unset;
        }
      }
    }
  }

  .body {
    padding-top: 50px;
    width: 100%;
    flex:1;
    display: flex;
    // align-items: center;
    justify-content: center;
    background-image: linear-gradient(to bottom,#ebbba7, #cfc7f8);

    .content {
      flex:1;
      width: 80%;
      display: flex;
      flex-direction: column;
      align-items: center;
      // justify-content: center;

      .searchInput {
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: center;

        .searchContent {
          width: 100%;
          display: flex;
          align-items: center;
          justify-content: center;
          position: relative;
          flex-direction: column;

          .tips {
            top: 50px;
            position: absolute;
            width: 40%;
            z-index: 9999;
            background-color: rgb(250, 247, 247);
            border-radius: 15px;

            .tip {
              margin: 0;
              padding: 5px;
              padding-left: 25px;
              box-sizing: border-box;
              width: 100%;
              height: 40px;
              font-size: 20px;
              line-height: 30px;
              border: 1px solid rgb(248, 238, 238);
              border-collapse: collapse;
              cursor: pointer;
            }

            .clearHistorySearch {
              margin: 0;
              padding: 5px;
              text-align: center;
              box-sizing: border-box;
              width: 100%;
              height: 40px;
              font-size: 20px;
              line-height: 30px;
              border: 1px solid rgb(216, 216, 216);
              border-collapse: collapse;
              cursor: pointer;
            }
          }

          :deep(.el-input) {
            box-sizing: border-box;
            width: 40%;
            height: 50px;
            border: 1px solid #ccc; // 自定义边框颜色
            border-radius: 4px; // 自定义边框圆角
            background-color: #f9f9f9; // 自定义背景颜色

            // 自定义输入框内的文本样式
            :deep(.el-input__inner) {
              padding: 10px; // 自定义内边距
              font-size: 16px; // 自定义字体大小
              color: #333; // 自定义文字颜色
            }

            // 自定义清除按钮样式
            :deep(.el-input__clear) {
              color: #999; // 自定义清除按钮的颜色
            }
          }

        }
      }

      .tag {
        width: 50%;
        display: flex;
        flex-direction: column;
        // border: 1px solid rgb(163, 162, 162);
        border-radius: 20px;
        background-color: #ece5e2;
        margin-top: 45px;

        .selectCompany {
          box-sizing: border-box;
          width: 100%;
          border-radius: 10px;
          padding: 2%;
          display: flex;
          justify-content: center;

          .selectCompanyLeft {
            width: 150px;

            .selectCompanyLeftTxt {
              height: 50px;
              line-height: 50px;
              text-align: center;
              font-size: 20px;
              font-weight: 700;
              text-overflow: ellipsis;
              white-space: nowrap;
            }
          }

          .selectCompanyRight {
            width: 750px;
            display: flex;
            flex-wrap: wrap;

            .selectTag {
              height: 30px;
              line-height: 30px;
              box-sizing: border-box;
              padding-left: 10px;
              padding-right: 10px;
              font-size: 15px;
              text-align: center;
              background-color: rgb(227, 227, 227);
              border-radius: 50px;
              margin: 10px;
            }

            .selected {
              background-color: rgb(74, 201, 251);
            }
          }
        }
      }


      .companyList {
        margin-top: 50px;
        width: 1000px;
        display: flex;
        flex-wrap: wrap;

        .company {
          background-color: #eaecf0;
          border-radius: 10px;
          box-sizing: border-box;
          // border: 1px solid rgb(213, 213, 213);
          width: 450px;
          height: 100px;
          display: flex;
          margin-bottom: 25px;
          align-items: center;
          justify-content: center;

          .companyInfo {
            width: 350px;
            margin-left: 20px;
            height: 100px;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;

            p {
              margin: 5px;
              overflow: hidden;
              display: -webkit-box;
              -webkit-line-clamp: 2;
              -webkit-box-orient: vertical;
            }

            .infoName {
              width: 100%;
              font-size: 20px;
            }

            .address {
              width: 100%;
              font-size: 15px;
              -webkit-line-clamp: 1;
            }
          }
        }

        .company_left {
          margin-right: 50px;
        }

        .company_right {
          margin-left: 50px;
        }
      }

      .pagination {
        margin-top: 50px;
        margin-bottom: 100px;
        width: 1000px;
        display: flex;
        align-items: center;
        justify-content: center;
      }
    }
  }
}
</style>